package com.wgts.wgts.mvp.view.activity.homapge;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.netmodle.resp.HomeEntryEntity;

import java.util.ArrayList;

public class HomeEntryAdapter extends RecyclerView.Adapter<HomeEntryAdapter.EntranceViewHolder> {


    private Context context;

    private ArrayList<HomeEntryEntity> arrayList;

    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public HomeEntryAdapter(Context context, ArrayList<HomeEntryEntity> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public EntranceViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new EntranceViewHolder(LayoutInflater.from(context).inflate(R.layout.entrance_view_holder, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull EntranceViewHolder entranceViewHolder, final int i) {
        entranceViewHolder.imgEntry.setImageResource(arrayList.get(i).img);
        entranceViewHolder.tvTitle.setText(arrayList.get(i).title);

        entranceViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onClick(v, i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public class EntranceViewHolder extends RecyclerView.ViewHolder{

        public View itemView;

        public ImageView imgEntry;

        public TextView tvTitle;

        public EntranceViewHolder(@NonNull View itemView) {
            super(itemView);
            imgEntry = itemView.findViewById(R.id.img_entry);
            tvTitle = itemView.findViewById(R.id.tv_title);
            this.itemView = itemView;
        }
    }

    public interface  OnClickListener{
        void onClick(View view, int postion);
    }
}
